package com.bootdo.common.controller;

import com.bootdo.common.config.Constant;
import com.bootdo.common.service.impl.PatientService;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.form.CancelForm;
import com.bootdo.form.YuyueForm;
import com.bootdo.system.domain.DeptDO;
import com.bootdo.system.domain.SubscribeRecordDO;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.domain.YuYueDocDO;
import com.bootdo.util.DatePattern;
import com.bootdo.util.DateUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/system/patient")
@Controller
public class PatientController {
    private static final String prefix = "system/patient";
    @Autowired
    private PatientService patientService;

    @GetMapping
    String patient() {
        return prefix + "/patient";
    }

    @GetMapping("/guahao")
    String guahao() {
        return prefix + "/guahao";
    }

    /**
     * 跳转到预约区号页面
     */
    @GetMapping("/add")
    String add(@RequestParam("userId") Long userId, Model model) {
        model.addAttribute("userId", userId);
        return  prefix + "/add";
    }

    /**
     * 获取预约历史数据
     */
    @ApiOperation(value="获取患者预约列表", notes="")
    @ResponseBody
    @GetMapping("/list")
    public List<SubscribeRecordDO> list(String docName, Integer status) {
        return patientService.list(docName, status);
    }

    /**
     * 获取医生数据
     */
    @ApiOperation(value="获取预约医生数据", notes="")
    @ResponseBody
    @GetMapping("/list1")
    public List<YuYueDocDO> list1(String keshi) {
        return patientService.list1(keshi);
    }

    /**
     * 预约
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    R add(YuyueForm form) {
        // 参数校验
        if(StringUtils.isEmpty(form.getSeeTime())){
            return R.error("请输入就诊时间！");
        }
        String seeTime = form.getSeeTime().replace("T", " ");
        Date st = DateUtils.parse(seeTime, DatePattern.YMD_HM);
        return patientService.add(form.getDocUserId(), st);
    }

    /**
     * 取消预约
     * @return
     */
    @ResponseBody
    @PostMapping("/cancel")
    R cancel(CancelForm form) {
        return patientService.cancel(form.getId());
    }

    /**
     * 管理员批量取消预约
     */
    @PostMapping("/batchCancel")
    @ResponseBody
    @RequiresPermissions("system:patient:batchCancel")
    public R batchCancel(@RequestParam("ids[]") Long[] deptIds) {
        return patientService.batchCancel(deptIds);
    }
}
